﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;

using SvD_Templates;

namespace WindowsFormsApplication1
{
    class fnXML
    {
        private settings set = new settings();

        public List<String> getTemplatesList()
        {
            XElement xl;
            xl = XElement.Load(@set["local_path"].ToString() + set["templates_xml"].ToString());
            List<String> listTemplates = new List<string>();

            var query = from c in xl.Descendants("template") select c.Element("name").Value;


            foreach (var q in query)
            {
                listTemplates.Add(q.ToString());
            }

            return listTemplates;
        }

        public List<String> getPhonebook()
        {
            List<String> phonebook = new List<String>();
            XElement xl;
            xl = XElement.Load(@set["local_path"].ToString() + set["phonebook_xml"].ToString());

            var query = from c in xl.Descendants("contact") select c.Element("inf").Value;


            foreach (var q in query)
            {
                phonebook.Add(q.ToString());
            }

            return phonebook;
        }

        public List<String> getInboxes()
        {
            List<String> inboxes = new List<String>();
            XElement xl;
            xl = XElement.Load(@set["local_path"].ToString() + set["inboxes_xml"].ToString());

            var query = from c in xl.Descendants("inbox") select c.Element("name").Value;


            foreach (var q in query)
            {
                inboxes.Add(q.ToString());
            }

            return inboxes;
        }

        public List<String> getSites()
        {
            List<String> sites = new List<String>();
            XElement xl;
            xl = XElement.Load(@set["local_path"].ToString() + set["sites_xml"].ToString());

            var query = from c in xl.Descendants("site") select c.Element("name").Value;


            foreach (var q in query)
            {
                sites.Add(q.ToString());
            }

            return sites;
        }

        public List<String> getCompanies()
        {
            List<String> companies = new List<String>();
            XElement xl;
            xl = XElement.Load(@set["local_path"].ToString() + set["companies_xml"].ToString());

            var query = from c in xl.Descendants("company") select c.Element("name").Value;


            foreach (var q in query)
            {
                companies.Add(q.ToString());
            }

            return companies;
        }

        public List<String> getKeywords()
        {
            List<String> keywords = new List<String>();
            XElement xl;
            xl = XElement.Load(@set["local_path"].ToString() + set["keywords_xml"].ToString());

            var query = from c in xl.Descendants("keyword") select c.Element("name").Value;

            foreach (var q in query)
            {
                keywords.Add(q.ToString());
            }

            return keywords;
        }

        public datosTemplate getTemplate(String template)
        {
            XElement xl;
            datosTemplate __dt = new datosTemplate();
            xl = XElement.Load(@set["local_path"].ToString() + set["templates_xml"].ToString());

            var query = from c in xl.Descendants("template")
                        where c.Element("name").Value.Contains(template)
                        select new {
                            title = c.Element("title").Value,
                            company = c.Element("company").Value,
                            issue = c.Element("issue").Value,
                            ts = c.Element("ts").Value,
                            keyword = c.Element("keyword").Value,
                            dispatch = c.Element("dispatch").Value,
                            usertime = c.Element("usertime").Value,
                            altcontact = c.Element("altcontact").Value,
                            warmtransfer = c.Element("warmtransfer").Value,
                            severity = c.Element("severity").Value,
                            lang = c.Element("lang").Value, 
                            url = c.Element("url").Value,
                            isimac = c.Element("isimac").Value,
                        };


            foreach (var q in query)
            {
                __dt.Titulo = q.title;
                __dt.user_Company = q.company;
                __dt.Detalles_Ticket = q.issue;
                __dt.Acciones_Realizadas = q.ts;
                __dt.Keyword = q.keyword;
                __dt.Caja_Asignacion = q.dispatch;
                __dt.Mejor_Hora_Localizar = q.usertime;
                __dt.Contact_Alterno = q.altcontact;
                __dt.Warm_Transfer = q.warmtransfer;
                __dt.Severidad = q.severity;
                __dt.Idioma = q.lang;
                __dt.url = q.url;
                __dt.Es_IMAC = q.isimac;
            }
           

            return __dt;
        }
    }
}
